<template>
  <van-icon
    :color="value === 1 ? 'red' : '#777'"
    :name="value == 1 ? 'good-job' : 'good-job-o'"
    @click="onClick"
  />
</template>

<script>
import { addLike, deleteLike } from '@/api/article'
export default {
  name: 'LikeArticle',
  props: {
    value: {
      type: Number,
      require: true
    },
    articleId: {
      type: [Number, String, Object],
      required: true
    }

  },
  components: {},
  data () {
    return {
      loading: false
    }
  },
  created () {},
  mounted () {},
  methods: {
    async onClick () {
      try {
        if (this.loading) return
        this.loading = true
        if (this.value === 1) {
        //  取消
          await deleteLike(this.articleId)
        } else {
        //  点赞
          await addLike(this.articleId)
        }
        this.$toast.success(this.value ? '取消点赞' : '点赞成功')
        this.$emit('input', this.value === 1 ? 0 : 1)
        this.loading = false
      } catch (error) {
        this.$toast('操作失败，请重试')
      }
    }
  },
  watch: {},
  computed: {}
}
</script>

<style lang="less" scoped>
</style>
